package com.yuanhua.sim.service.impl;

import com.yuanhua.core.base.entity.MsgRecBean;
import com.yuanhua.core.base.entity.MsgReqBean;
import com.yuanhua.core.base.entity.MsgResBean;
import com.yuanhua.sim.netty.utils.SessionUtils;
import com.yuanhua.sim.service.NettyService;
import io.netty.channel.Channel;
import org.springframework.stereotype.Service;

@Service
public class NettyServiceImpl implements NettyService {

    /**
     * 发送消息
     * @param msgReqBean
     */
    @Override
    public Boolean sendMsg(MsgReqBean msgReqBean) {
        Channel c = SessionUtils.getChannel(msgReqBean.getTouserid());
        boolean result = true;
        if(c==null){
            //人不在线，回调给网关，让其下线
            MsgResBean res=new MsgResBean();
            res.setStatus(1);
            res.setMsg(msgReqBean.getTouserid() +",不在线");
            result = false;
        }else{
            MsgRecBean res=new MsgRecBean();
            res.setFromuserid(msgReqBean.getFromuserid());
            res.setMsg(msgReqBean.getMsg());
            c.writeAndFlush(res);
        }
        return result;
    }
}
